package com.yxp423.ssmempl.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.extra.servlet.multipart.UploadFile;
import cn.hutool.extra.servlet.multipart.UploadSetting;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.qiniu.http.Response;
import com.yxp423.ssmempl.dto.QiNiuResult;
import com.yxp423.ssmempl.service.IQiniuService;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;

/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-07-22 18:33
 * @Version: 1.0
 */


@RestController()
@RequestMapping("/user/")
@Api(tags = "个人信息")
public class UserInfoController {

	@Autowired
	IQiniuService qiniuService;

	@Autowired
	private Gson gson;

	@PutMapping(":{userId}")
	@ApiOperation("用户修改密码")
	public AjaxResult changePassword(@PathVariable @ApiParam("用户id") String userId,
									 @ApiParam("密码") String password) {
		AjaxResult result = new AjaxResult();


		return result;
	}


	@PostMapping("avatar/{id}")

	@ResponseBody
	public AjaxResult changeImg(@PathVariable("id") String userId, MultipartFile file) throws IOException {

		AjaxResult result = new AjaxResult();

		//获得文件内容
		InputStream fileInfo = file.getInputStream();

		//获得原始文件名
		String filename = file.getOriginalFilename();


		Response response = qiniuService.uploadFile(fileInfo);

		if (response.isOK()){

			QiNiuResult qiNiuResult = gson.fromJson(response.bodyString(), QiNiuResult.class);

			result.setSuccess(true);
			result.setData(qiNiuResult);

		}else {

			result.setSuccess(false);
			result.setMsg("上传失败");
		}
		return result;
	}

}
